/***********************************************************************
 * Module:  ResizeState.java
 * Author:  Nikola
 * Purpose: Defines the Class ResizeState
 ***********************************************************************/

package genedit.controller;

import genedit.view.graph.GraphWorkArea;
import genedit.view.graph.SelectionHandleHandler.Handle;

import java.awt.Cursor;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;

public class ResizeState extends State {
	private Point2D start;
	private double globalX = 0;
	private double globalY = 0;
	private Handle handle;
   public void mouseReleased(MouseEvent e) {
      // TODO: implement
	   Point2D lastPosition = e.getPoint();
		context.pointToUserSpace(lastPosition); 
		double xDiff = 0; 
		double yDiff = 0; 
		switch (handle) {
			case North:
				yDiff= start.getY()-lastPosition.getY();
				globalY+=yDiff;
				break;
			case South:
				yDiff= lastPosition.getY()-start.getY();
				globalY+=yDiff;
				break;
			case East:
				xDiff= lastPosition.getX()-start.getX();
				globalX+=xDiff;
				break;
			case West:
				xDiff= start.getX()-lastPosition.getX();
				globalX+=xDiff;
				break;
			case SouthEast:
				if (e.getButton() == MouseEvent.BUTTON1 && !e.isShiftDown()) {
					xDiff= lastPosition.getX()-start.getX();
					globalX+=xDiff;
					yDiff= lastPosition.getY()-start.getY();
					globalY+=yDiff;
				}
				else {
					yDiff = xDiff = Math.max(lastPosition.getX()-start.getX(),lastPosition.getY()-start.getY());
					globalX+=xDiff;
					globalY+=yDiff;
				}
				break;
			case NorthWest:
				if (e.getButton() == MouseEvent.BUTTON1 && !e.isShiftDown()) {
					yDiff= start.getY()-lastPosition.getY();
					globalY+=yDiff;
					xDiff= start.getX()-lastPosition.getX();
					globalX+=xDiff;
				}
				else {
					yDiff = xDiff = Math.max(start.getX()-lastPosition.getX(),start.getY()-lastPosition.getY());
					globalX+=xDiff;
					globalY+=yDiff;
				}
				break;
			case SouthWest:
				if (e.getButton() == MouseEvent.BUTTON1 && !e.isShiftDown()) {
					yDiff= lastPosition.getY()-start.getY();
					globalY+=yDiff;
					xDiff= start.getX()-lastPosition.getX();
					globalX+=xDiff;
				}
				else {
					yDiff = xDiff = Math.max(start.getX()-lastPosition.getX(),lastPosition.getY()-start.getY());
					globalX+=xDiff;
					globalY+=yDiff;
				}
				break;
			case NorthEast:
				if (e.getButton() == MouseEvent.BUTTON1 && !e.isShiftDown()) {
					yDiff= start.getY()-lastPosition.getY();
					globalY+=yDiff;
					xDiff= lastPosition.getX()-start.getX();
					globalX+=xDiff;
				}
				else {
					yDiff = xDiff = Math.max(lastPosition.getX()-start.getX(),start.getY()-lastPosition.getY());
					globalX+=xDiff;
					globalY+=yDiff;
				}
				break;
			default:
				break;
		}
		ResizeCommand newResize = new ResizeCommand(true,globalX,globalY, handle);
		newResize.setDocument(context.getView());
		newResize.setMoveByX(xDiff);
		newResize.setMoveByY(yDiff);
		newResize.setResizedElements(context.getView().getModel().getSelectedElements());
		context.getView().getModel().getInvoker().executeCommand(newResize);
		context.getController().setCurrentState(new SelectionState(context));
		context.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
	
   }
   
   /** @param e */
   public void mouseDragged(MouseEvent e) {
      // TODO: implement
	   Point2D lastPosition = e.getPoint();
			context.pointToUserSpace(lastPosition); 
			double xDiff = 0; 
			double yDiff = 0; 
			switch (handle) {
			case North:
				yDiff= start.getY()-lastPosition.getY();
				globalY+=yDiff;
				break;
			case South:
				yDiff= lastPosition.getY()-start.getY();
				globalY+=yDiff;
				break;
			case East:
				xDiff= lastPosition.getX()-start.getX();
				globalX+=xDiff;
				break;
			case West:
				xDiff= start.getX()-lastPosition.getX();
				globalX+=xDiff;
				break;
			case SouthEast:
				if (!e.isShiftDown()) {
				xDiff= lastPosition.getX()-start.getX();
				globalX+=xDiff;
				yDiff= lastPosition.getY()-start.getY();
				globalY+=yDiff;
				}
				else {
					yDiff = xDiff = Math.max(lastPosition.getX()-start.getX(),lastPosition.getY()-start.getY());
					globalX+=xDiff;
					globalY+=yDiff;
				}
				break;
			case NorthWest:
				if (!e.isShiftDown()) {
				yDiff= start.getY()-lastPosition.getY();
				globalY+=yDiff;
				xDiff= start.getX()-lastPosition.getX();
				globalX+=xDiff;
				}
				else {
					yDiff = xDiff = Math.max(start.getX()-lastPosition.getX(),start.getY()-lastPosition.getY());
					globalX+=xDiff;
					globalY+=yDiff;
				}
				break;
			case SouthWest:
				if (!e.isShiftDown()) {
				yDiff= lastPosition.getY()-start.getY();
				globalY+=yDiff;
				xDiff= start.getX()-lastPosition.getX();
				globalX+=xDiff;
				}
				else {
					yDiff = xDiff = Math.max(start.getX()-lastPosition.getX(),lastPosition.getY()-start.getY());
					globalX+=xDiff;
					globalY+=yDiff;
				}
				break;
			case NorthEast:
				if (!e.isShiftDown()) {
				yDiff= start.getY()-lastPosition.getY();
				globalY+=yDiff;
				xDiff= lastPosition.getX()-start.getX();
				globalX+=xDiff;
				}
				else {
					yDiff = xDiff = Math.max(lastPosition.getX()-start.getX(),start.getY()-lastPosition.getY());
					globalX+=xDiff;
					globalY+=yDiff;
				}
				break;
			default:
				break;
			}
			ResizeCommand newResize = new ResizeCommand(false,globalX,globalY,handle);
			newResize.setDocument(context.getView());
			newResize.setMoveByX(xDiff);
			newResize.setMoveByY(yDiff);
			newResize.setResizedElements(context.getView().getModel().getSelectedElements());
			context.getView().getModel().getInvoker().executeCommand(newResize);
		start=lastPosition;
   }
   
   /** @param e */
   public void keyPressed(KeyEvent e) {
		int keycode = e.getKeyCode(); 
		if (keycode == KeyEvent.VK_ESCAPE)
		{
			
			ResizeCommand newResize = new ResizeCommand(false, globalX, globalY, handle);
			newResize.setDocument(context.getView());
			newResize.setMoveByX(-globalX);
			newResize.setMoveByY(-globalY);
			newResize.setResizedElements(context.getView().getModel().getSelectedElements());
			context.getView().getModel().getInvoker().executeCommand(newResize);
		
			context.getController().setCurrentState(new SelectionState(context));
			context.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
	
			// restart
			globalX = 0;
			globalY = 0;
			ResizeCommand.translateConnectionPoints=null;
		}
	}
   
   /** @param context */
   public ResizeState(GraphWorkArea context ,Handle handle, Point2D start) {
		super(context);
		this.setStart(start);
		this.handle = handle;
 
}

public Point2D getStart() {
	return start;
}

public void setStart(Point2D start) {
	this.start = start;
}


}